<?php
/**
 * 用户打卡签到类
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/20 0020
 * Time: 下午 12:25
 */
namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Model\Backstage\Customers;
use App\Model\Backstage\Staffattendance;
use App\Tool\Error;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SigninController extends Controller
{
    public $SigninType = ['看房','勘察','跑盘','派单','培训','上班','下班','迟到','早退','午餐','晚餐','加班','外出','请假','休假','陪看','其他'];

    /**
     *  打卡类型
     */
    public function getSigninType(){
            $data['type'] = $this->SigninType;
            $data['currenttime'] = date('H-i-s Y:m:d');
         return $this->success('ok',$data);
    }



    /**上班打卡
     * @param Request $request
     */
    public function goToWork(Staffattendance $staffattendance)
    {
        $dayTime = Carbon::today()->toDateString();
        $res = $staffattendance->gotoWork($dayTime);
        if ($res === -1) {
            return $this->error(Error::PUNCH_ALREADY_UP);
        }
        if (!$res) {
            return $this->error(Error::PUNCH_FAIL);
        }
        return $this->success('ok', $res);
    }

    /** 下班打卡
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function afterWork(Request $request, Staffattendance $staffattendance)
    {
        $dayTime = Carbon::today()->toDateString();
        $res = $staffattendance->afterWork($dayTime, $request, $this->province, $this->city, $this->IP);
        if ($res === -1) {
            return $this->error(Error::PUNCH_ALREADY_DOWN);
        }
        if (!$res) {
            return $this->error(Error::PUNCH_FAIL);
        }
        return $this->success('ok', '打卡成功！');
    }
    /**
     *打卡列表
     */
    public function Rankings(Request $request)
    {
        $map = [];
        // 判断有没有统计开始时间和结束时间  如果有按时间来查询  如果没有 查询当前月份
        if($request->input('start_time')){
            $map[] = ['start_work_time', '>=', strtotime($request->input('start_time').' 00:00:00')];
        }
        if($request->input('end_time')){
            $map[] = ['start_work_time', '<=', strtotime($request->input('end_time').' 23:59:59')];
        }
        if(empty($map)){
            $map[] = ['start_work_time', '>=', strtotime(date('Y-m-d',strtotime('-1 month')).' 00:00:00')];
            $map[] = ['start_work_time', '<=', time()];
        }
        //按签到类型查找
        if($request->input('sign_type')){
            $map[] = ['sign_type', $request->input('sign_type')];
        }
        //按部门查找
        if($request->input('s_d_id')){
            $map[] = ['s_d_id', $request->input('s_d_id')];
        } else {
            $map[] = ['s_d_id',  Auth::user()['u_d_id']];
        }
        //按员工查找
        if($request->input('employee_id')){
            $map[] = ['employee_id', $request->input('employee_id')];
        } else if (!$request->input('s_d_id') && !$request->input('employee_id')) {
            $map[] = ['employee_id',  Auth::user()['u_id']];
        }
        $classStaff = new Staffattendance();
        $list = $classStaff->staffRankings($map, env('PAGE_LIMIT_ADMIN'));
        if (!$list){
            return $this->error(Error::NULL_DATA);
        }
        $start = date('Y-m-d',strtotime('-1 month'));
        $end = date('Y-m-d',time());
        return $this->success('ok', $list, $start, $end);
    }


}